<!--  -->
<template>
  <div class="study">
    <!-- <button @click="toLogin">登录</button> -->
    <div v-if="token" class="top-nav">
      <div
        class="nav-item"
        v-for="(item, index) in list"
        :key="item"
        :class="{ active: current == index }"
        @click="change(index)"
      >
        <div class="item-text">{{ item }}</div>
        <div class="item-dot"></div>
      </div>
    </div>
    <div v-if="publishedCourse.length == 0" class="empty-box">
      <img src="../assets/img/img-placeholder.png" alt="" />
    </div>
    <div v-if="!token" class="btn-login-box">
      <div class="btn-login" @click="toLogin">登录查看</div>
    </div>
    <div v-if="token" class="courses-box">
      <div v-if="current == 0" class="course-item-box">
        <div class="course-item" v-for="item in publishedCourse" :key="item.id">
          <div class="course-thumb">
            <img :src="item.thumb" />
          </div>
          <div class="course-body">
            <div class="course-title">{{ item.title }}</div>
            <div class="at">订阅时间&nbsp;1 个月前</div>
          </div>
        </div>
      </div>
      <div v-if="current == 1" class="course-item-box">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getCollectedCourses"
        >
          <div class="course-item" v-for="item in like" :key="item.id">
            <div class="course-thumb">
              <img :src="item.thumb" />
            </div>
            <div class="course-body">
              <div class="course-title">{{ item.title }}</div>
              <div class="at">订阅时间&nbsp;1 个月前</div>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <TechSupport></TechSupport>
    <FooterNav type="study"></FooterNav>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import TechSupport from "@/components/TechSupport.vue";
import FooterNav from "@/components/FooterNav.vue";
import Vue from "vue";
import { List } from "vant";
Vue.use(List);
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {
    TechSupport,
    FooterNav,
  },
  data() {
    //这里存放数据
    return {
      token: "",
      publishedCourse: [],
      current: 0,
      list: ["订阅课程", "收藏课程"],
      like: [],
      type: "vod",
      page: 0,
      size: 6,
      loading: false,
      finished: false,
      total: 0,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    toLogin() {
      this.$router.push({
        path: "/login-password",
        query: {
          url: window.location.href,
        },
      });
    },
    getCollectedCourses() {
      // /api/v2/member/courses/like?type=vod&page=1&size=10&loading=true
      this.$request
        .get("/api/v2/member/courses/like", {
          params: {
            type: this.type,
            page: this.page + 1,
            size: this.size,
            loading: this.loading,
          },
        })
        .then((res) => {
          this.total = res.data.total;
          // 0 表示没有更多的数据了
          if (res.data.total == 0) {
            this.finished = true;
          }
          this.page = res.data.current_page;
          this.loading = false;
          // 追加的逻辑
          this.like = [...this.like, ...res.data.data];
        });
    },
    change(index) {
      this.current = index;
      if (this.current == 0) {
        let res = this.$request
          .get("/api/v2/member/courses")
          .then((res) => {
            console.log(res);
            this.publishedCourse = res.data.data;
          })
          .catch((err) => {
            console.log(err);
          });
      } 
      // else if (this.current == 1) {
        // let res = this.$request
        //   .get("/member/courses/like?type=vod&page=1&size=10&loading=true")
        //   .then((res) => {
        //     console.log(res);
        //     this.publishedCourse = res.data.data;
        //   })
        //   .catch((err) => {
        //     console.log(err);
        //   });
        // let like = this.$request
        //   .get("api/v2/member/courses/like", {
        //     params: {
        //       type: "vod",
        //       page: "1",
        //       size: "10",
        //       loading: true,
        //     },
        //   })
        //   .then((res) => {
        //     console.log(res.data.data);
        //     this.like = res.data.data;
        //   });
        // this.getCollectedCourses();
      // }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  async created() {
    let token = localStorage.getItem("myToken");
    this.token = token;
    if (token) {
      let res = await this.$request
        .get("/api/v2/member/courses")
        .then((res) => {
          this.publishedCourse = res.data.data;
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang="less" scoped>
.study {
  width: 100%;
  .top-nav {
    width: 100%;
    height: auto;
    float: left;
    box-sizing: border-box;
    position: sticky;
    top: 0;
    z-index: 999;
    display: flex;
    padding-top: 17px;
    padding-bottom: 8px;
    background-color: #fff;
    .nav-item.active {
      flex: 1;
      position: relative;
      .item-text {
        color: #333;
        font-weight: 600;
        width: 100%;
        height: auto;
        float: left;
        font-size: 16px;
        line-height: 16px;
        text-align: center;
        padding-bottom: 9px;
      }

      .item-dot {
        background-color: #3ca7fa;
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -5px;
        width: 10px;
        height: 3px;
        border-radius: 2px;
      }
    }

    .nav-item {
      flex: 1;
      position: relative;
      .item-text {
        width: 100%;
        height: auto;
        float: left;
        font-size: 16px;
        font-weight: 400;
        color: #666;
        line-height: 16px;
        text-align: center;
        padding-bottom: 9px;
      }

      .item-dot {
        position: absolute;
        bottom: 0;
        left: 50%;
        margin-left: -5px;
        width: 10px;
        height: 3px;
        background: #fff;
        border-radius: 2px;
      }
    }
  }
  .empty-box {
    width: 100%;
    padding: 50px 0;
    text-align: center;
    img {
      width: 120px;
      height: 120px;
    }
  }
  .btn-login-box {
    width: 100%;
    padding: 0 20px;
    .btn-login {
      width: 100%;
      height: 46px;
      box-sizing: border-box;
      line-height: 46px;
      background: #3ca7fa;
      border-radius: 4px;
      float: left;
      text-align: center;
      color: #fff;
      font-size: 16px;
      font-weight: 500;
    }
  }
  .courses-box {
    width: 100%;
    height: auto;
    float: left;
    background-color: #fff;
    box-sizing: border-box;
    padding: 15px;
    .course-item-box {
      .course-item {
        width: 100%;
        height: auto;
        float: left;
        margin-bottom: 15px;
        display: flex;
        .course-thumb {
          margin-right: 10px;
          img {
            width: 120px;
            height: 90px;
            border-radius: 4px;
          }
        }

        .course-body {
          flex: 1;
          padding-top: 5px;
          .course-title {
            width: 100%;
            height: 40px;
            float: left;
            font-size: 15px;
            font-weight: 500;
            color: #171923;
            line-height: 20px;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            margin-bottom: 22px;
          }

          .at {
            width: 100%;
            height: auto;
            float: left;
            font-size: 12px;
            font-weight: 400;
            color: #999;
            line-height: 12px;
          }
        }
      }
    }
  }
}
</style>
